import time

from DrissionPage import Chromium
from DrissionPage import SessionPage


def visit_website(url, i, phone):
    try:
        # 用 d 模式创建页面对象（默认模式）
        # tab = Chromium().latest_tab
        tab = Chromium(9333).latest_tab

        # 跳转到登录页面
        tab.get(url)
        time.sleep(3)

        # 定位聊天框输入手机号
        tab.ele('xpath://textarea[contains(@class, "imlp-component-typebox-input")]').input(phone)

        # 点击发送按钮
        tab.ele('xpath://div[contains(@class, "imlp-component-typebox-send-btn")]').click()

        try:
            # 等待弹窗出现并点击关闭按钮
            pass
        except Exception as e:
            # 如果弹窗没有出现，就什么都不做
            pass
        time.sleep(30)

    except Exception as e:
        # 如果发生错误，将执行结果添加到共享列表
        pass


def boom(phone):
    """模仿浏览器，请求api信息"""
    with open('api.txt', 'r') as file:
        urls = file.readlines()
        # 计算链接地址条数
        n_urls = len(urls)

    for i, v in enumerate(urls):
        visit_website(v, i, phone)
        pass


if __name__ == '__main__':
    boom("123456")